package cn.tedu._06interceptor.config;

import cn.tedu._06interceptor.interceptors.MyInterceptor1;
import cn.tedu._06interceptor.interceptors.MyInterceptor2;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MvcConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 策略1：注册拦截器:默认拦截所有请求;
        //registry.addInterceptor(new MyInterceptor1());

        // 策略2：拦截指定的单个请求,比如只拦截登录请求;
        //registry.addInterceptor(new MyInterceptor1())
        //        .addPathPatterns("/v1/users/login");

        // 策略3：拦截指定的多个请求,比如拦截登录、注册请求;
        //registry.addInterceptor(new MyInterceptor1())
        //        .addPathPatterns("/v1/users/login", "/v1/users/reg");

        // 策略4：拦截指定的多个请求,比如拦截用户模块的所有请求,**表示任意路径;
        //registry.addInterceptor(new MyInterceptor1())
        //        .addPathPatterns("/v1/users/**");

        // 策略5：排除指定的请求,比如拦截用户模块除了登录之外的所有请求;
        //registry.addInterceptor(new MyInterceptor1())
        //        .addPathPatterns("/v1/users/**")
        //        .excludePathPatterns("/v1/users/login");

        /*
            队列：FIFO(First In First Out)
            堆栈：LIFO(Last In First Out)
            preHandle:         [pre1,pre2,pre3]
            postHandle:        [post1,post2,post3]
            afterCompletion:   [after1,after2,after3]
         */

        registry.addInterceptor(new MyInterceptor1());
        registry.addInterceptor(new MyInterceptor2());
    }
}
